package com.airlines.crewassignment.service.airport.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.entity.dict.Select2;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.service.airport.AirportService;
import com.airlines.crewassignment.service.dict.Select2Service;

/**
 * @author xxxx
 * @date 2018年6月12日 
 * @description
 */
@Service
public class AirportServiceImpl implements AirportService{
	@Autowired
	private Select2Service select2Service;

	@Override
	public String getShortCityLine(List<FlightInfo> list,List<Select2> cityAbbrList) {
		if(CollectionUtils.isEmpty(list)){
			return "";
		}
		StringBuffer sb = new StringBuffer();
		if(cityAbbrList == null){
			cityAbbrList = select2Service.getAirportCityAbbr();		
		}
		
		List<String> airportList = new ArrayList<>();
		airportList.add("");
		for(FlightInfo f: list){
			String lastAirport = airportList.get(airportList.size()-1);
			String departureAirportCityAbbr = select2Service.getText(cityAbbrList, f.getDepartureAirport());
			if(!StringUtils.equals(lastAirport, departureAirportCityAbbr)){
				airportList.add(departureAirportCityAbbr);
				sb.append(departureAirportCityAbbr);
			}
			String arrivalAirportCityAbbr = select2Service.getText(cityAbbrList, f.getArrivalAirport());
			sb.append(arrivalAirportCityAbbr);
			airportList.add(arrivalAirportCityAbbr);
		}
			
		return sb.toString();
	}

	@Override
	public String getAirportCodeLine(List<FlightInfo> list) {
		if(CollectionUtils.isEmpty(list)){
			return "";
		}
		StringBuffer sb = new StringBuffer();
		Set<String> airportSet = new HashSet<>();
		for(FlightInfo f: list){
			int size1 = airportSet.size();
			airportSet.add(f.getDepartureAirport());
			int size2 = airportSet.size();
			
			if(size2>size1){
				sb.append(f.getDepartureAirport()+"/");
			}
			
			airportSet.add(f.getArrivalAirport());
			int size3 = airportSet.size();
			if(size3>size2){
				sb.append(f.getArrivalAirport()+"/");
			}
			
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	
	
}
